package kr.hyunist.androidcomplete01saeildamil.ch13;

import java.text.*;
import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class DateTimeTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datetimetest);

		Refresh();

		Button btn = (Button) findViewById(R.id.datetimetestbtn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Refresh();
			}

		});
	}

	private void Refresh() {
		StringBuilder time = new StringBuilder();

		long epoch = System.currentTimeMillis();
		time.append("epoch = " + epoch + "\n");

		Calendar cal = new GregorianCalendar();
		time.append("now = "
				+ String.format("%d년 %d월 %d일 %d시 %d분\n",
						cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
						cal.get(Calendar.DAY_OF_MONTH),
						cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)));

		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
		time.append("now = " + sdf.format(now) + "\n");

		Calendar tom = new GregorianCalendar();
		tom.add(Calendar.DAY_OF_MONTH, -7);
		Date tomdate = tom.getTime();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd");
		time.append("tomorrow : " + sdf2.format(tomdate) + "\n");

		time.append("boot = " + UpTime(SystemClock.elapsedRealtime()));
		time.append("run = " + UpTime(SystemClock.uptimeMillis()));
		time.append("thread = " + UpTime(SystemClock.currentThreadTimeMillis()));

		TextView result = (TextView) findViewById(R.id.datetimetesttv);
		result.setText(time.toString());
	}

	private String UpTime(long msec) {
		long sec = msec / 1000;
		String result;
		result = String.format("%d일 %d시 %d분 %d초\n", sec / 86400,
				sec / 3600 % 24, sec / 60 % 60, sec / 60);
		return result;
	}
}
